package com.example.seckill.controller;

import com.example.seckill.pojo.User;
import com.example.seckill.service.IUserService;
import com.example.seckill.vo.RespBean;
import com.example.seckill.vo.RespBeanEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;

@RestController
@RequestMapping("/register")
public class RegisterController {
    @Autowired
    private IUserService userService;
    @RequestMapping("/doRegister")
    public RespBean doRegister(String nickname, String password, String salt, Long id, Integer loginCount, Long registerDate, HttpServletRequest request, HttpServletResponse response){
        response.setHeader("Access-Control-Allow-Origin","*");
        User user = new User();
        user.setPassword(password);
        user.setSalt(salt);
        user.setNickname(nickname);
        user.setId(id);
        user.setLoginCount(loginCount);
        user.setRegisterDate(new Date(registerDate));
        System.out.println(user);

        boolean b = userService.doRegister(user, request, response);
        if (b){
            return RespBean.success();
        }else{
            return RespBean.error(RespBeanEnum.REGISTER_ERROR);
        }
    }
}
